package com.example.tiptravel.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.example.tiptravel.R;
import com.example.tiptravel.commom.base.BaseFragment;
import com.example.tiptravel.entity.post.PostBean;
import com.example.tiptravel.presenter.post.PostPresenter;
import com.example.tiptravel.view.activity.PublishActivity;
import com.example.tiptravel.view.adapter.post.PostAdapter;
import com.example.tiptravel.view.impl.post.PostView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class PostFragment extends BaseFragment implements PostView {

    @BindView(R.id.post_recyclerview)
    XRecyclerView xRecyclerView;
    private List<PostBean.DataEntity.ResultEntity> data = new ArrayList<>();
    private PostAdapter adapter;

    @BindView(R.id.iv_post_menu)
    ImageView ivMenu;
    @BindView(R.id.iv_post_addtravel)
    ImageView ivAddTravel;

    public PostFragment() {
        // Required empty public constructor
    }


    public static PostFragment newInstance() {

        Bundle args = new Bundle();

        PostFragment fragment = new PostFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_post;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        xRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PostAdapter(data, getContext());
        xRecyclerView.setAdapter(adapter);
        initData();
    }


    @Override
    protected void initData() {
        PostPresenter postPresenter = new PostPresenter(this);
        postPresenter.getPostData();
    }

    @Override
    public void onSuccess(PostBean postBean) {
        if (postBean != null) {
            data.addAll(postBean.getData().getResult());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(Throwable e) {

    }

    @Override
    public void onComplete() {

    }

    @OnClick({R.id.iv_post_addtravel, R.id.iv_post_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_post_menu:
                break;
            case R.id.iv_post_addtravel:
                Intent intent = new Intent(getActivity(), PublishActivity.class);
                startActivity(intent);
                break;
        }
    }

}
